﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using App.CustomServerControls;
using App.Data;
using App.Core.Extensions;

/// <summary>
/// Summary description for BindDropdownList
/// </summary>
public class BindDropdownList
{    
    /// <summary>
    /// Binds a Dropdownlist to a DataTable
    /// </summary>
    /// <param name="ddl"></param>
    /// <param name="dt"></param>
    /// <param name="displayField"></param>
    /// <param name="valueField"></param>
    public static void BindDropDownList(DropDownList ddl, DataTable dt, String displayField, String valueField)
    {

        if (dt == null || dt.Rows.Count <= 0)
        {
            if (ddl.Items.Count > 0)
                ddl.Items.Clear();
            ddl.DataSource = null;
            ddl.DataBind();
        }
        else
        {
            ddl.DataSource = dt.DefaultView;
            ddl.DataTextField = displayField;
            ddl.DataValueField = valueField;
            ddl.DataBind();
        }
        ddl.Items.Insert(0, new ListItem(String.Empty, String.Empty));

    }
    /// <summary>
    /// Binds a Dropdownlist Controls to a Collection
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="ddl"></param>
    /// <param name="entities"></param>
    /// <param name="displayField"></param>
    /// <param name="valueField"></param>
    public static void BindToList<T>(DropDownList ddl, IList<T> entities, String displayField, String valueField)
        where T : System.Data.Objects.DataClasses.EntityObject
    {
        if (entities == null || entities.Count == 0)
        {
            if (ddl.Items.Count > 0)
                ddl.Items.Clear();
            ddl.DataSource = null;
        }
        else
        {
            ddl.DataSource = entities;
            ddl.DataTextField = displayField;
            ddl.DataValueField = valueField;
        }
        ddl.DataBind();
        ddl.Items.Insert(0, new ListItem(String.Empty, String.Empty));
    }

    public static void Countries(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var countries = from P in context.Countries orderby P.Name select P;
        BindToList(ddl, countries == null ? null : countries.ToList(), "Name", "ID");
    }
    public static void Cities(DropDownList ddl, int countryID)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var cities = from P in context.Cities where P.CountryID == countryID orderby P.Name select P;
        BindToList(ddl, cities == null ? null : cities.ToArray(), "Name", "ID");
    }
    public static void Cities(DropDownListX ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var cities = from P in context.Cities.Include("Country") 
                     orderby P.Country.Name, P.Name select P;
        int prevCountry = 0;
        foreach (City city in cities)
        {            
            if (city.CountryID != prevCountry)
            {                
                ddl.AddGroup(city.Country.Name);
            }
            ///Add the Village            
            ddl.Items.Add(new ListItem(city.Name.HtmlEncode(), city.ID.ToString()));
            prevCountry = city.CountryID;
        }
    }
    public static void ProjectStatuses(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var statuses = from P in context.ProjectStatuses select P;
        BindToList(ddl, statuses == null ? null : statuses.ToArray(), "Name", "ID");
    }
    public static void PropertyTypes(DropDownList ddl, bool inserBlankRowAtFirst)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var types = (from P in context.PropertyTypes where P.ID != ConfigReader.OtherPropertyTypeID select P).OrderBy(P => P.Name);
        if (inserBlankRowAtFirst)
            BindToList(ddl, types == null ? null : types.ToArray(), "Name", "ID");
        else
        {
            ddl.DataSource = types;
            ddl.DataValueField = "ID";
            ddl.DataTextField = "Name";
            ddl.DataBind();
        }
    }
    public static void PropertyTypes(DropDownList ddl, int businessTypeID)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var types = from P in context.PropertyTypes where P.BusinessTypeID == businessTypeID && P.ID != ConfigReader.OtherPropertyTypeID select P;
        BindToList(ddl, types == null ? null : types.ToArray(), "Name", "ID");
    }

    public static void BindProjectFileTypes(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var types = from P in context.ProjectFileTypes select P;
        BindToList(ddl, types == null ? null : types.ToArray(), "Name", "ID");
    }

    public static void Zones(DropDownList ddl, int cityID)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var zones = from P in context.Zones where P.CityID == cityID orderby P.Name select P;
        BindToList(ddl, zones == null ? null : zones.ToArray(), "Name", "ID");
    }

    public static void Designations(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var designations = from P in context.Designations select P;
        BindToList(ddl, designations == null ? null : designations.ToList(), "Name", "ID");
    }

    public static void Currencies(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var currencies = from P in context.Currencies select P;
        BindToList(ddl, currencies == null ? null : currencies.ToList(), "Name", "ID");
    }

    public static void Customers(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var customers = from P in context.Customers where P.IsApproved && !P.IsDeleted orderby P.Name select P;
        BindToList(ddl, customers == null ? null : customers.ToList(), "Name", "ID");
    }
    public static void Customers(DropDownList ddl, int businessTypeID)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var customers = from P in context.Customers where P.BusinessTypeID == businessTypeID && P.IsApproved && !P.IsDeleted orderby P.Name select P;
        BindToList(ddl, customers == null ? null : customers.ToList(), "Name", "ID");
    }

    public static void BusinessReresentative(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var customers = from P in context.Customers where !P.IsIndividual && P.IsApproved && !P.IsDeleted orderby P.Name select P;
        BindToList(ddl, customers == null ? null : customers.ToList(), "Name", "ID");
    }

    public static void RentStatuses(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var rentStatuses = from P in context.RentStatus orderby P.Name select P;
        BindToList(ddl, rentStatuses == null ? null : rentStatuses.ToList(), "Name", "ID");
    }

    public static void RentTypes(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var rentTypes = from P in context.RentTypes orderby P.Name select P;
        BindToList(ddl, rentTypes == null ? null : rentTypes.ToList(), "Name", "ID");
    }

    public static void SubscriptionTypes(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var packages = from P in context.Packages where P.IsActive orderby P.ID select P;
        BindToList(ddl, packages == null ? null : packages.ToList(), "Name", "ID");
    }

    public static void Newsletters(DropDownList ddl)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var newsLetters = from P in context.NewsLetters where P.IsActive orderby P.Title select P;
        BindToList(ddl, newsLetters == null ? null : newsLetters.ToList(), "Title", "ID");
    }

    public static void Contacts(DropDownList ddl, int _CustomerID)
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var contacts = from P in context.Contacts where P.CustomerID == _CustomerID && !P.IsDeleted orderby P.Name select P;
        BindToList(ddl, contacts == null ? null : contacts.ToList(), "Name", "ID");
    }
}